<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="终端编号" prop="clientNo">
        <el-input
          v-model="queryParams.clientNo"
          placeholder="请输入终端编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="终端名称" prop="clientName">
        <el-input
          v-model="queryParams.clientName"
          placeholder="请输入终端名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入机构ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="最后通讯">
        <el-date-picker
          v-model="daterangeUpdateTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['device:pressureSensorClient:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:pressureSensorClient:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:pressureSensorClient:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['device:pressureSensorClient:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pressureSensorClientList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="终端编号" align="center" prop="clientNo" />
      <el-table-column label="终端名称" align="center" prop="clientName" />
      <el-table-column label="机构ID" align="center" prop="deptId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="在线时长" align="center" prop="onlineTimes" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="所在地址" align="center" prop="address" />
      <el-table-column label="管理人id" align="center" prop="userId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSetup(scope.row)"
            v-hasPermi="['device:pressureSensorClient:edit']"
          >配置</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['device:pressureSensorClient:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:pressureSensorClient:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改压力传感器设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="终端编号" prop="clientNo">
          <el-input v-model="form.clientNo" placeholder="请输入终端编号" />
        </el-form-item>
        <el-form-item label="终端名称" prop="clientName">
          <el-input v-model="form.clientName" placeholder="请输入终端名称" />
        </el-form-item>
        <el-form-item label="机构ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入机构ID" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="在线时长" prop="onlineTimes">
          <el-input v-model="form.onlineTimes" placeholder="请输入在线时长" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="所在地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入所在地址" />
        </el-form-item>
        <el-form-item label="管理人id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入管理人id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 设备配置 -->
    <el-dialog :title="title" :visible.sync="setupOpen" width="50%" append-to-body>
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="变送器数据" name="data">
          <el-row :gutter="10">
            <el-col :span="8">
              <el-card class="box-card">
                <div slot="header" class="clearfix">
                  <span>变送器一般信息</span>
                </div>
                <el-form ref="form" :model="transmitterInfoForm" :rules="transmitterInfoRules" label-width="60px">
                  <el-form-item label="地址" prop="userId">
                    <el-input v-model="transmitterInfoForm.userId" placeholder="请输入地址" />
                  </el-form-item>
                  <el-form-item label="波特率" prop="userId">
                    <el-input v-model="transmitterInfoForm.userId" placeholder="请输入波特率" />
                  </el-form-item>
                  <el-form-item label="单位" prop="userId">
                    <el-input v-model="transmitterInfoForm.userId" placeholder="请输入单位" />
                  </el-form-item>
                  <el-form-item label="小数点" prop="userId">
                    <el-input v-model="transmitterInfoForm.userId" placeholder="请输入小数点" />
                  </el-form-item>
                  <el-form-item label="压力值" prop="userId">
                    <el-input v-model="transmitterInfoForm.userId" placeholder="请输入管压力值" />
                  </el-form-item>
                  <el-form-item label="量程L" prop="userId">
                    <el-input v-model="transmitterInfoForm.userId" placeholder="请输入量程L" />
                  </el-form-item>
                  <el-form-item label="量程H" prop="userId">
                    <el-input v-model="transmitterInfoForm.userId" placeholder="请输入量程H" />
                  </el-form-item>
                  <el-form-item label="滤波值" prop="userId">
                    <el-input v-model="transmitterInfoForm.userId" placeholder="请输入滤波值" />
                  </el-form-item>
                </el-form>
              </el-card>
            </el-col>
            <el-col :span="8">
              <el-card class="box-card">
                <div slot="header" class="clearfix">
                  <span>电流输出控制项目</span>
                </div>
                <el-form ref="form" :model="currentOutputForm" :rules="currentOutputRules" label-width="100px">
                  <el-form-item label="开关1吸合值" prop="userId">
                    <el-input v-model="currentOutputForm.actuation_v1" placeholder="请输入开关1吸合值" />
                  </el-form-item>
                  <el-form-item label="开关1释放值" prop="userId">
                    <el-input v-model="currentOutputForm.release_v1" placeholder="请输入开关1释放值" />
                  </el-form-item>
                  <el-form-item label="开关2吸合值" prop="userId">
                    <el-input v-model="currentOutputForm.actuation_v1" placeholder="请输入开关2吸合值" />
                  </el-form-item>
                  <el-form-item label="开关2释放值" prop="userId">
                    <el-input v-model="currentOutputForm.release_v1" placeholder="请输入开关2释放值" />
                  </el-form-item>
                  <el-form-item label="开关3吸合值" prop="userId">
                    <el-input v-model="currentOutputForm.actuation_v1" placeholder="请输入开关3吸合值" />
                  </el-form-item>
                  <el-form-item label="开关3释放值" prop="userId">
                    <el-input v-model="currentOutputForm.release_v1" placeholder="请输入开关3释放值" />
                  </el-form-item>
                  <el-form-item label="开关4吸合值" prop="userId">
                    <el-input v-model="currentOutputForm.actuation_v1" placeholder="请输入开关4吸合值" />
                  </el-form-item>
                  <el-form-item label="开关4释放值" prop="userId">
                    <el-input v-model="currentOutputForm.release_v1" placeholder="请输入开关4释放值" />
                  </el-form-item>
                </el-form>
              </el-card>
            </el-col>
            <el-col :span="8">
              <el-card class="box-card">
                <div slot="header" class="clearfix">
                  <span>ADC及清零</span>
                </div>
                <el-form ref="form" :model="adcForm" :rules="adcRules" label-width="100px">
                  <el-form-item label="ADC实时值" prop="realTimeValue">
                    <el-input v-model="adcForm.realTimeValue" placeholder="请输入ADC时值" />
                  </el-form-item>
                  <el-form-item label="零位偏移值" prop="zeroOffsetValue">
                    <el-input v-model="adcForm.zeroOffsetValue" placeholder="请输入零位偏移值" />
                  </el-form-item>
                  <el-form-item label="标识固定" prop="fixedIdentification">
                    <el-input v-model="adcForm.fixedIdentification" placeholder="请输入标识固定1234:" />
                  </el-form-item>
                  <el-form-item style="text-align: center;">
                    <el-button type="primary" @click="zeroPositionClear">零位清零</el-button>
                  </el-form-item>
                </el-form>
              </el-card>
              <el-card class="box-card" shadow="never" style="margin-top: 2px;">
                <el-divider content-position="left">实时压力显示</el-divider>
                <el-statistic :value="realTimePressureValue">
                  <template slot="suffix">
                    <span>
                      MPa
                    </span>
                  </template>
                </el-statistic>
              </el-card>
            </el-col>
          </el-row>
        </el-tab-pane>
        <el-tab-pane label="端口设置" name="port">配置管理</el-tab-pane>
      </el-tabs>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="setupOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPressureSensorClient, getPressureSensorClient, delPressureSensorClient, addPressureSensorClient, updatePressureSensorClient } from "@/api/device/pressureSensorClient";

export default {
  name: "PressureSensorClient",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 压力传感器设备表格数据
      pressureSensorClientList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 管理人id时间范围
      daterangeCreateTime: [],
      // 管理人id时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        clientNo: null,
        clientName: null,
        deptId: null,
        remark: null,
        onlineTimes: null,
        longitude: null,
        latitude: null,
        address: null,
        userId: null,
        createTime: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        clientNo: [
          { required: true, message: "终端编号不能为空", trigger: "blur" }
        ],
      },
      setupOpen: false,
      activeName: 'data',
      transmitterInfoForm: {

      },
      transmitterInfoRules: [],
      currentOutputForm: {

      },
      currentOutputRules: [],
      adcForm: {

      },
      adcRules: [],
      realTimePressureValue: '0.0'
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询压力传感器设备列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeUpdateTime && '' != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] = this.daterangeUpdateTime[0];
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      }
      listPressureSensorClient(this.queryParams).then(response => {
        this.pressureSensorClientList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        clientNo: null,
        clientName: null,
        deptId: null,
        remark: null,
        onlineTimes: null,
        longitude: null,
        latitude: null,
        address: null,
        userId: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加压力传感器设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPressureSensorClient(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改压力传感器设备";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePressureSensorClient(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPressureSensorClient(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除压力传感器设备编号为"' + ids + '"的数据项？').then(function() {
        return delPressureSensorClient(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/pressureSensorClient/export', {
        ...this.queryParams
      }, `pressureSensorClient_${new Date().getTime()}.xlsx`)
    },
    /** 配置按钮操作 */
    handleSetup(row) {
      this.setupOpen = true
    }
  }
};
</script>
